package com.ngkj.zmfptback.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.sys.initData.servicie.read.AreaService;
import com.ngkj.zmfptback.business.bean.BusinessEntity;
import com.ngkj.zmfptback.business.service.read.BusinessReadService;
import com.ngkj.zmfptback.business.service.write.BusinessWriteService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ChenHN
 * @since 2021/12/27 creation
 **/
@RestController
public class BusinessController {

    @Resource
    private AreaService areaService;

    @Resource
    private BusinessReadService businessReadService;

    @Resource
    private BusinessWriteService businessWriteService;



    @GetMapping("/business/getSqByQywzid")
    public CommonReslut getSqByQywzid(String qywzid){
        try {
            List<BusinessEntity> businessEntities = businessReadService.getSqByQywzid(qywzid);
            return new CommonReslut(ResultEnum.ok,businessEntities);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @GetMapping("/business/getBusinessPage")
    public CommonReslut getBusinessPage(BusinessEntity business, int pageNo, int pageSize) {
        try {
            Page<BusinessEntity> page = new Page<>(pageNo, pageSize);
            IPage<Map<String, Object>> businessPage = businessReadService.getBusinessPage(business, page);
            return new CommonReslut(ResultEnum.ok, businessPage);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/business/addBusiness")
    public CommonReslut addBusiness(@RequestBody BusinessEntity business) {
        try {
            String cityCode = business.getCityCode();
            String qywzid = business.getQywzid();
            String[] splits = qywzid.split(";");
            Long id = areaService.getAreaIdByAreaCode(cityCode);
            splits[splits.length-1] = id.toString();
            Stream<String> splits1 = Stream.of(splits);
            String qywzids = splits1.collect(Collectors.joining(";"));
            business.setQywzid(qywzids);
            business.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            businessWriteService.addBusiness(business);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/business/editBusiness")
    public CommonReslut editBusiness(@RequestBody BusinessEntity business) {
        try {
            String cityCode = business.getCityCode();
            if (!StringUtils.isEmpty(cityCode)) {
                Long id = areaService.getAreaIdByAreaCode(cityCode);
                String qywzid = business.getQywzid();
                String[] splits = qywzid.split(";");
                splits[splits.length-1] = id.toString();
                Stream<String> splits1 = Stream.of(splits);
                String qywzids = splits1.collect(Collectors.joining(";"));
                business.setQywzid(qywzids);
            }
            businessWriteService.editBusiness(business);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }


    @DeleteMapping("/business/deleteBusiness")
    public CommonReslut deleteBusiness(Long sqlsh) {
        try {
            businessWriteService.deleteBusiness(sqlsh);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

}
